package cn.alpha.intell.rbtlib.share;

import android.app.Dialog;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;

import java.util.ArrayList;
import java.util.List;

import cn.alpha.intell.rbtlib.R;
import cn.alpha.intell.rbtlib.utils.NetWorkUtil;
import cn.alpha.intell.rbtlib.utils.ScreenUtil;
import cn.alpha.intell.rbtlib.view.widget.common.BadNetView;

/**
 * Created by zhangxiliang on 2016/11/1.
 */
public class ShareDialog extends Dialog{

    private View dialogView;
    ContactAdapter mContactAdapter;

    RecyclerView mRecyclerview;
    Button mSelectOk;
    ImageView mCloseIV;
    List<Contact> mContactList;

    private OkCallBack okCallBack;

    private Context mContext;

    public ShareDialog(Context context) {
        super(context, R.style.alpha_dialog);
        this.mContext = context;

        dialogView = LayoutInflater.from(context).inflate(R.layout.dialog_share, null);
        //getWindow().getAttributes().gravity= Gravity.CENTER_HORIZONTAL;


        setCanceledOnTouchOutside(true);
        setCancelable(true);
        setContentView(dialogView);
        mRecyclerview=(RecyclerView)dialogView.findViewById(R.id.id_recyclerview);
        mSelectOk=(Button)dialogView.findViewById(R.id.select_ok);
        mCloseIV=(ImageView)dialogView.findViewById(R.id.colse_iv);

        mContactList=new ArrayList<>();
        mContactAdapter=new ContactAdapter(mContext,mContactList);

        mContactAdapter.setOnItemClickListener(new ContactAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(Contact info, int position) {
                mSelectOk.setEnabled(true);
            }
        });
        mCloseIV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        mRecyclerview.setLayoutManager(new StaggeredGridLayoutManager(1,StaggeredGridLayoutManager.HORIZONTAL));
        mRecyclerview.setAdapter(mContactAdapter);

        mSelectOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(okCallBack!=null){
                    okCallBack.onClick(ShareDialog.this,mContactAdapter.getSelectedContact());
                }
            }
        });

        ShareContentResolver.getInstance().getContactFromContentProvider(mContext,  new ShareContentResolver.ProviderCallBack() {
            @Override
            public void onSuccess(List<Contact> contactList) {
                mContactList.addAll(contactList);
                mContactAdapter.notifyDataSetChanged();

            }
        });

        getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
        getWindow().getAttributes().width = (int)ScreenUtil.dpToPx(context,724);
    }


    public void setOkCallBack(OkCallBack okCallBack) {
        this.okCallBack = okCallBack;
    }

    public interface OkCallBack{
        public void onClick(Dialog dialog,Contact contact);
    }

    public ShareDialog(Context context, int themeResId) {
        super(context, themeResId);
    }
}
